﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Twofolders
{
    public partial class NewFolderPairForm : Form
    {
        internal FolderPair NewFolderPair { get; set; }

        private List<FolderPair> m_existingFolderPairs;

        public NewFolderPairForm(List<FolderPair> existingFolderPairs)
        {
            m_existingFolderPairs = existingFolderPairs;
            InitializeComponent();
            OnUserInputChanged();
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            NewFolderPair = new FolderPair(m_nameTextBox.Text.Trim(), 
                Utility.AppendDirectorySeparator(m_sourceTextBox.Text), 
                Utility.AppendDirectorySeparator(m_destinationTextBox.Text), 
                m_toDeleteCheckBox.Checked);
        }

        private void NameTextBox_TextChanged(object sender, EventArgs e)
        {
            OnUserInputChanged();
        }

        private void SourceTextBox_TextChanged(object sender, EventArgs e)
        {
            OnUserInputChanged();
        }

        private void DestinationTextBox_TextChanged(object sender, EventArgs e)
        {
            OnUserInputChanged();
        }

        private void OnUserInputChanged()
        {
            m_addButton.Enabled = false;

            if (string.IsNullOrEmpty(m_nameTextBox.Text.Trim())) return;
            if (m_nameTextBox.Text.Contains("|")) return;

            foreach (var folderPair in m_existingFolderPairs)
            {
                if (m_nameTextBox.Text.ToLower().Trim() == folderPair.Name.ToLower()) return;
            }
            
            if (Utility.isDirectoryValid(m_sourceTextBox.Text) &&
                Utility.isDirectoryValid(m_destinationTextBox.Text))
            {
                if (Utility.AppendDirectorySeparator(m_sourceTextBox.Text).ToLower() !=
                    Utility.AppendDirectorySeparator(m_destinationTextBox.Text).ToLower())
                {
                    m_addButton.Enabled = true;
                }
            }
        }
    }
}
